import React from 'react';
import { connect } from 'dva';
import moment from 'moment'
import { Button, Table, Form, Input, Popconfirm,DatePicker, Select,TreeSelect,   } from 'antd';
const FormItem = Form.Item;
const Option = Select.Option;
const TreeNode = TreeSelect.TreeNode;
const RangePicker = DatePicker.RangePicker;
import AddPlanModal from '../../components/patrol/AddPlanModal';
import PatrolListModal from '../../components/patrol/PatrolListModal';
import ViewResultModal from '../../components/patrol/ViewResultModal';

function PatrolPlan ({ dispatch, patrolPlan, loading, form }) {

    const { getFieldDecorator, getFieldsValue, getFieldValue } = form;
    const { patrolItem,currentItem , modalVisible, modalType,roomList,newKey,userList, modalViewVisible,detailList,cycletime
    ,patrolId,assetCurrent,assetCurrentSize,assetTotal} = patrolPlan

    const columns = [{
        title: '巡检名称',
        dataIndex: 'name',
        key: 'name',
        width: 100,
    },{
        title: '重复周期',
        dataIndex: 'cycleName',
        key: 'cycleName',
        width: 100,
    },{
        title: '巡检开始日期',
        dataIndex: 'startDate',
        key: 'startDate',
        width: 130,
        render: (value, row, index) => {
            return (
                value?new moment(value).format("YYYY-MM-DD"):''
             )
        }
    },{
        title: '巡检结束日期',
        dataIndex: 'endDate',
        key: 'endDate',
        width: 130,
        render: (value, row, index) => {
            return (
                value?new moment(value).format("YYYY-MM-DD"):''
            )
        }
    },{
        title: '责任人',
        dataIndex: 'operatiorName',
        key: 'operatiorName',
        width: 80,
    },{
        title: '巡检时间',
        dataIndex: 'patrolTime',
        key: 'patrolTime',
        width: 220,
    },{
        title: '备注',
        dataIndex: 'desc',
        key: 'desc',
        width: 120,
    },{
        title: '操作',
        key: 'operation',
        width: 150,
        render: (text, record) => (
            <span>
                <span>
                    <a onClick={onEditItem.bind(null, record)}>编辑</a>
                    <span className="ant-divider" />
                    <Popconfirm title="确认删除？" onConfirm={deleteHandler.bind(null, record.id_)}>
                    <a>删除</a>
                    </Popconfirm>
                </span>
                <span className="ant-divider" />
                <a onClick={onViewPatrolDetailItem.bind(null, record.id_)}>巡检单据</a>
            </span>
        ),
    }];

    const pagination = {
        current:patrolPlan.current,
        pageSize:patrolPlan.currentSize,
        total: patrolPlan.total,
        showSizeChanger: true,
        showTotal: (total => '共' + patrolPlan.total + '条'),
        onShowSizeChange(current, size) {
            dispatch({
                type: 'patrolPlan/queryList',
                payload: { ...getFieldsValue(),...{'pageNum':current, 'pageSize':size} },
            });
        },
        onChange(current) {
            dispatch({
                type: 'patrolPlan/queryList',
                payload: { ...getFieldsValue(),...{'pageNum':current, 'pageSize':patrolPlan.currentSize} },
            });
        },
    };
    const editHandler = (id, values) => {
        dispatch({
            type: 'patrolPlan/edit',
            payload: { id, ...values },
        });
    }
    const onEditItem = (record) => {
        dispatch({
            type: 'patrolPlan/qryById',
            payload: record,
        })
    }
    const submitHandle = () => {
        dispatch({
            type: 'patrolPlan/queryList',
            payload: { ...getFieldsValue() },
        });
    }
    function handleClear(){
      form.resetFields();
    }

    // 查看巡检单据列表
    const onViewPatrolDetailItem = (id) => {
        dispatch({
            type: 'patrolPlan/viewPatrolDetail',
            payload:{inspectPlanId:id},
        })
    }
    // 查看巡检详情
    const onViewItem = (id) => {
        dispatch({
            type: 'patrolPlan/onViewItem',
            payload:{inspectPlanId:id},
        })
    }
    const addHandler = () => {
        dispatch({
        type: 'patrolPlan/showModal',
        payload: {
            modalType: 'create',
        },
        })
    }
    function deleteHandler(id) {
        dispatch({
        type: 'patrolPlan/remove',
        payload: {id:id,search:{...getFieldsValue()}},
        });
    }

    const modalProps = {
        item: modalType === 'create' ? {} : currentItem,
        roomList: roomList,
        userList:userList,
        dispatch:dispatch,
        cycletime:cycletime,
        visible: modalVisible,
        type: modalType,
        maskClosable: false,
        confirmLoading: loading,
        title: `${modalType === 'create' ? '新增巡检计划' : '编辑巡检计划'}`,
        wrapClassName: 'vertical-center-modal',
        newKey : newKey,
        onOk (data) {
            dispatch({
                type: `patrolPlan/${modalType}`,
                payload: data,
            })
        },
        onCancel () {
            dispatch({
                type: 'patrolPlan/hideModal',
            })
        },
        onRadioChange (data) {
            dispatch({
                type: 'patrolPlan/onRadioChange',
                payload: data,
            })
        },

    }

    const modalPatrolProps = {
        visible: patrolPlan.patrolModalVisible,
        patrolList:patrolPlan.patrolList,
        dispatch:dispatch,
        inspectPlanId:patrolPlan.inspectPlanId,
        current:patrolPlan.patrolCurrent,
        currentSize:patrolPlan.patrolCurrentSize,
        total:patrolPlan.patrolTotal,
        maskClosable: false,
        confirmLoading: loading,
        title: '查看巡检单',
        wrapClassName: 'vertical-center-modal',
        newKey : newKey,
        onCancel () {
            dispatch({
                type: 'patrolPlan/hidePatrolModal',
            })
        },
        viewResult (data) {
            dispatch({
                type: 'patrolPlan/getPatrolResult',
                payload:data,
            })
        },
    }
    const modalResultProps = {
        item: patrolItem,
        visible: modalViewVisible,
        detailList:detailList,
        patrolId:patrolId,
        dispatch:dispatch,
        assetCurrent:assetCurrent,
        assetCurrentSize:assetCurrentSize,
        assetTotal:assetTotal,
        maskClosable: false,
        confirmLoading: loading,
        title: ' 查看巡检详情:'+patrolItem.name,
        wrapClassName: 'vertical-center-modal',
        newKey : newKey,
        onCancel () {
            dispatch({
                type: 'patrolPlan/hideViewModal',
                payload: { detailList:[],assetCurrent:1,assetTotal:0}
            })
        },
    }

    const loopRoom = data => data.map((item) => {
      return <Option key={item.id_ + ''}>{item.name}</Option>
    });

    return (
         <div style={{backgroundColor:'#fff'}}>
          <div style={{minHeight:'100%'}}>
              <div style={{width:'100%',backgroundColor:'#fff',lineHeight:'40px',paddingLeft:'20px',paddingTop:'10px'}}>
                  <Form layout="inline" >
                    <FormItem>
                        { getFieldDecorator('cycleType')(
                           <Select
                                showSearch
                                style={{ width: 200 }}
                                placeholder="选择重复周期"
                                optionFilterProp="children"
                           >
                              <Option value="">全部</Option>
                              <Option value="1">每天</Option>
                              <Option value="2">每周</Option>
                              <Option value="3">每月</Option>
                            </Select>
                           )}
                    </FormItem>

                    <FormItem>
                        {getFieldDecorator('name')(
                            <Input type="text" placeholder="巡检计划名称" style={{ width:'200px', marginRight: '3%' }} />
                        )}
                    </FormItem>

                    <FormItem>
                        <Button type="primary" size='large' onClick={submitHandle} className='ant-search-btn'>查询</Button>
                    </FormItem>
                    <FormItem>
                        <Button size="large" htmlType="reset" onClick={handleClear}>清空</Button>
                    </FormItem>

                    <div style={{marginTop:'-40px',textAlign:'right',paddingRight:'10px'}}>
                        <FormItem>
                            <Button type="primary" size='large' onClick={addHandler}>新增巡检计划</Button>
                        </FormItem>
                    </div>
                </Form>
            </div>

            <div style={{width:'100%',padding:"30px 20px",}}>
                <Table columns={columns} rowKey={record => record.id_} loading={loading} dataSource={patrolPlan.list} pagination={pagination}
                />
            </div>
            <AddPlanModal {...modalProps} />
            <PatrolListModal {...modalPatrolProps} />
            <ViewResultModal {...modalResultProps} />

            </div>
        </div>
    );
}

function mapStateToProps(state) {
  return { patrolPlan : state.patrolPlan,loading:state.loading.models.patrolPlan};
}

PatrolPlan = Form.create()(PatrolPlan);
export default connect(mapStateToProps)(PatrolPlan);
